/*
 * @描述: 
 * @创建者: kilomChou
 * @Date: 2021-04-11 19:16:48
 * @最后编辑: kilomChou
 * @LastEditTime: 2021-10-27 11:58:06
 */

import { Canvas, director, math, resources, UITransform } from "cc";
import { initProto, Proto } from "../common/data/Proto";
import Game from "../common/game/Game";
import { GameMgr } from "./manager/GameMgr";
import { Conn } from "./net/Conn";
import { WorldMgr } from "./manager/WorldMgr";
import { initXlsxVo, loadData } from "../common/data/XlsxVo";
import kcoder from "k-coder";
import { WorldMod } from "./model/WorldMod";
import BaseModel from "./model/BaseModel";
import { SnakeMod } from "./model/SnakeMod";
import { FoodMod } from "./model/FoodMod";
import ModuleGlobal from "../ModuleGlobal";

/** 游戏应用全局 */
export default class GameAppCls {

    /** 单例 */
    private constructor() { }
    /** 实例 */
    static readonly ins = new GameAppCls();

    /** 是否开启调试模式 */
    isDebug = true;
    /** 是否已经初始化了 */
    isInited = false;
    /** 是否正在开始中 */
    starting = false;
    /** 主画布大小 */
    mainCanvasSize: math.Size;
    /** 世界管理器 */
    worldMgr: WorldMgr;
    /** 游戏逻辑端 */
    game: Game;
    readonly MODS: BaseModel[] = [];

    private _mainCanvas: Canvas;
    private _gameMgr: GameMgr;

    /** 主画布 */
    get mainCanvas() { return this._mainCanvas; }
    set mainCanvas(cv: Canvas) { this._mainCanvas = cv; this.mainCanvasSize = cv.getComponent(UITransform).contentSize }
    /** 主摄像机 */
    get mainCamera() { return this._mainCanvas.cameraComponent; }
    /** 游戏管理器 */
    get gameMgr() { return this._gameMgr; }
    set gameMgr(mgr: GameMgr) { this._gameMgr = mgr; this.mainCanvas = mgr.getComponent(Canvas); }

    /** 初始化 */
    init() {
        this.isDebug && ModuleGlobal();
        initXlsxVo(kcoder);
        initProto(kcoder);
        resources.load("data/data", (err, asset) => {
            loadData(new Uint8Array(asset._nativeAsset))
            asset.decRef(true);
            this.initModel();
            this.isInited = true;
        })
    }

    /** 初始化model */
    initModel() {
        this.MODS.push(
            WorldMod,
            SnakeMod,
            FoodMod,
        );
    }

    /** 开始本地游戏 */
    startLocal(playerName: string) {
        this.starting = true;
        Conn.isLocal = true;
        this.game = new Game();
        this.game.conn(Conn);
        this.conn(playerName);
        this.starting = false;
    }

    /** 开始多人游戏 */
    startRemote(playerName: string, ip: string) {
        this.starting = true;
        Conn.connServer(ip, () => {
            //连接成功
            this.conn(playerName);
        }, () => {
            //连接失败
            alert("连接服务器失败");
            this.starting = false;
        });
    }

    /** 连接服务器成功加入游戏 */
    conn(playerName: string) {
        Conn.on(Proto.S2C_JOIN, data => {
            WorldMod.worldSize = data.worldSize;
            //载入场景
            director.loadScene("Game");
            this.starting = false;
        })
        Conn.send(Proto.C2S_JOIN, { name: playerName });
    }

    /** 游戏初始化完成准备接收数据了 */
    ready() {
        Conn.send(Proto.C2S_READY);
    }

}

/** 游戏应用全局 */
export const GameApp = GameAppCls.ins;